<template>
  <el-dialog
      top="5vh"
      width="500px"
      title="添加/编辑"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    >
    </SubmitForm>
  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  components: {
  },
  props: {
    deviceId: {
      type: [Number,String],
      default: 0
    }
  },
  data() {
    return {
      apiKey: 'port',
      dialogValue: false,
      ruleForm: {
        id: undefined,
      },
      defaultForm: {
        id: undefined,
        portName: '',
        portCode: '',
      },
      rules: {
        portName: [
          { required: true, message: '请输入枪口名称', trigger: 'blur' },
        ],
        portCode: [
          { required: true, message: '请输入枪口编号', trigger: 'blur' },
        ],
      },
    }
  },
  computed: {
    formList() {
      return [
        {
          label: '枪口名称',
          prop: 'portName',
          name: 'input',
          clearable: true,
          colSpan: 24
        },
        {
          label: '枪口编号',
          prop: 'portCode',
          name: 'input',
          clearable: true,
          disabled: () => this.ruleForm.id ? true : false,
          colSpan: 24
        },
      ]
    }

  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
      console.log(this.ruleForm)
    },
    onSave() {
      if (!this.$route.params.id) {
        return
      }
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm, stationId: this.deviceId }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    },
  }
}
</script>

<style scoped lang="scss">
</style>
